package com.cp.admin.core.util;

import com.cp.admin.api.utils.DateUtils;
import com.cp.admin.modular.code.service.TableService;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class As {
    private static long day = 0;
    private static long hour = 0;
    private static long minute = 0;
    private static long second = 0;

    public static boolean dayNotAlready = false;
    public static boolean hourNotAlready = false;
    public static boolean minuteNotAlready = false;
    public static boolean secondNotAlready = false;


    private String cqssc;

    private String xjssc;

    private String bjpk10;

    private String klssc;

    private String klsc;

    private String klft;

    public static void main(String[] args) {
        abc();


    }

    public static void abc(){
        long totalSeconds = 60 * 18 * 1;


        String dateStr = getWebsiteDatetime("http://www.baidu.com");

        System.out.println(dateStr);

        String endStr1 = dateStr.substring(0,14) + "08:00";//重庆时时彩封单时间
        String endStr2 = dateStr.substring(0,14) + "28:00";//重庆时时彩封单时间
        String endStr3 = dateStr.substring(0,14) + "48:00";//重庆时时彩封单时间

        String endStr4 = dateStr.substring(0,14) + "10:00";//重庆时时开奖单时间
        String endStr5 = dateStr.substring(0,14) + "30:00";//重庆时时开奖单时间
        String endStr6 = dateStr.substring(0,14) + "50:00";//重庆时时开奖单时间


        if(DateUtils.strToDate(dateStr).getTime() < DateUtils.strToDate(endStr1).getTime()){
            totalSeconds = DateUtils.strToDate(endStr1).getTime() - DateUtils.strToDate(dateStr).getTime();
            totalSeconds = totalSeconds/1000;
        }

        if(DateUtils.strToDate(dateStr).getTime() > DateUtils.strToDate(endStr4).getTime() &&
                DateUtils.strToDate(dateStr).getTime() < DateUtils.strToDate(endStr2).getTime()){
            totalSeconds = DateUtils.strToDate(endStr2).getTime() - DateUtils.strToDate(dateStr).getTime();
            totalSeconds = totalSeconds/1000;
        }

        if(DateUtils.strToDate(dateStr).getTime() > DateUtils.strToDate(endStr5).getTime() &&
                DateUtils.strToDate(dateStr).getTime() < DateUtils.strToDate(endStr3).getTime()){
            totalSeconds = DateUtils.strToDate(endStr3).getTime() - DateUtils.strToDate(dateStr).getTime();
            totalSeconds = totalSeconds/1000;
        }

        if(DateUtils.strToDate(dateStr).getTime() > DateUtils.strToDate(endStr6).getTime()){
            Date d = DateUtils.addHour(new Date(DateUtils.strToDate(dateStr).getTime()),1);

            if(DateUtils.strToDate(dateStr).getTime() < d.getTime()){
                totalSeconds = d.getTime() - DateUtils.strToDate(dateStr).getTime();
                totalSeconds = totalSeconds/1000;
            }

        }

        As.initData(totalSeconds);

        new Timer().schedule(new TimerTask() {
            public void run() {
                if (As.secondNotAlready) {
                    As.startCount();
                } else {
                    cancel();
                }

            }
        }, 0, 1000);
    }

    /**
      * 获取指定网站的日期时间
      * 
      * @param webUrl
      * @return
      */
    public static String getWebsiteDatetime(String webUrl){
        try {
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();// 读取网站日期时间 
            return DateUtils.dateToStr(new Date(ld), DateUtils.DATE_FORMAT_DATETIME);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 初始化赋值
     *
     * @param totalSeconds
     */
    public static void initData(long totalSeconds) {
        resetData();

        if (totalSeconds > 0) {
            secondNotAlready = true;
            second = totalSeconds;
            if (second >= 60) {
                minuteNotAlready = true;
                minute = second / 60;
                second = second % 60;
                if (minute >= 60) {
                    hourNotAlready = true;
                    hour = minute / 60;
                    minute = minute % 60;
                    if (hour > 24) {
                        dayNotAlready = true;
                        day = hour / 24;
                        hour = hour % 24;
                    }
                }
            }
        }
        System.out.println("初始格式化后——>" + day + "天" + hour + "小时" + minute
                + "分钟" + second + "秒");
    }

    public static void resetData() {
        day = 0;
        hour = 0;
        minute = 0;
        second = 0;
        dayNotAlready = false;
        hourNotAlready = false;
        minuteNotAlready = false;
        secondNotAlready = false;
    }

    /**
     * 计算各个值的变动
     *
     */
    public static void startCount() {
        if (secondNotAlready) {
            if (second > 0) {
                second--;
                if (second == 0 && !minuteNotAlready) {
                    secondNotAlready = false;
                }
            } else {
                if (minuteNotAlready) {
                    if (minute > 0) {
                        minute--;
                        second = 59;
                        if (minute == 0 && !hourNotAlready) {
                            minuteNotAlready = false;
                        }

                    } else {
                        if (hourNotAlready) {
                            if (hour > 0) {
                                hour--;
                                minute = 59;
                                second = 59;
                                if (hour == 0 && !dayNotAlready) {
                                    hourNotAlready = false;
                                }

                            } else {
                                if (dayNotAlready) {
                                    day--;
                                    hour = 23;
                                    minute = 59;
                                    second = 59;
                                    if (day == 0) {
                                        dayNotAlready = false;
                                    }

                                }
                            }
                        }
                    }

                }
            }

        }

        System.out.println(minute
                + "分钟" + second + "秒");

        if(minute == 0 && second == 1){
            abc();
        }

    }

}
